package cn.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class Ex2 {
    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            try{
                check();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        List<Integer> list = new ArrayList<>();
    }
    public static void check() throws Exception {
        int w = 100;
        int h = 50;
        Random rand = new Random();
        BufferedImage img = new BufferedImage(w, h, 2);
        Graphics2D g = img.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);

        Font font = Font.createFont(Font.TRUETYPE_FONT, new File("fonts/cb.ttf"));
        font = font.deriveFont(Font.PLAIN, 25);

        g.setFont(font);

        Color c = new Color(255, 0, 0);
        g.setColor(c);

        //验证码
        String str = getRandStr();
        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(180,256)));
        for (int i = 0; i < str.length(); i++) {
            int x = i * 25 + rand.nextInt(2, 6);
            int y = rand.nextInt(25, 45);
            g.drawString(str.substring(i, i + 1), x, y);
        }
        System.out.println(str);

        //干扰文字
        String str2 = getRandStr(rand.nextInt(8,20));
        for (int i = 0; i < str2.length(); i++) {
            g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(100,150)));
            int x = rand.nextInt(w);
            int y = rand.nextInt(h);
            font = font.deriveFont(1.0f * rand.nextInt(15, 20));
            g.setFont(font);
            g.drawString(str2.substring(i, i + 1), x, y);
        }

        for(int i=0;i<15;i++){
            int x1 = rand.nextInt(w);
            int x2 = rand.nextInt(w);
            int y1 = rand.nextInt(h);
            int y2 = rand.nextInt(h);
            g.setStroke(new BasicStroke(rand.nextInt(1,5)));
            g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(30,50)));
            g.drawLine(x1,y1,x2,y2);
        }

        g.dispose();
        ImageIO.write(img, "png", new File("temp/"+UUID.randomUUID().toString()+".png"));
    }

    public static String getRandStr() {
        return getRandStr(4);
    }

    public static String getRandStr(int len) {
        String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sbu = new StringBuilder();
        Random rand = new Random();
        for (int i = 0; i < len; i++) {
            int pos = rand.nextInt(0, letter.length());
            sbu.append(letter.substring(pos, pos + 1));
        }
        return sbu.toString();
    }
}
